﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace YLServerInterface.ServerConfig
{
    public class YLServerConfig
    {
        public string TTFPath { get; set; }
        public String WorkItemsFolder { get; set; }
        public String TemplateFolder { get; set; }
        public string YaZhiProjectFileName { get; set; }
        public String YaZhiExePath { get; set; }
        public String YaZhiPrepareMakeTime { get; set; }
        public String YaZhiMakeBtnName { get; set; }
        public String YaZhiDialogBoxYesBtn { get; set; }
        public String YaZhiRenderSelftTime { get; set; }
        public String YaZhiDialogBoxRenderSelftTime { get; set; }

        public string ImageCutterControlWidth { get; set; }
        public string ImageCutterControlHeight { get; set; }
        public string DefaultLoginName { get; set; }
        public string DefaultPwd { get; set; }
        /// <summary>
        /// 从配置文件中读取
        /// </summary>
        private YLServerConfig()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            WorkItemsFolder = config.AppSettings.Settings["WorkItemsFolder"].Value;
            TemplateFolder = config.AppSettings.Settings["TemplateFolder"].Value;
            YaZhiExePath = config.AppSettings.Settings["YaZhiExePath"].Value;
            YaZhiPrepareMakeTime = config.AppSettings.Settings["YaZhiPrepareMakeTime"].Value;
            YaZhiMakeBtnName = config.AppSettings.Settings["YaZhiMakeBtnName"].Value;
            YaZhiDialogBoxYesBtn = config.AppSettings.Settings["YaZhiDialogBoxYesBtn"].Value;
            YaZhiRenderSelftTime = config.AppSettings.Settings["YaZhiRenderSelftTime"].Value;
            YaZhiDialogBoxRenderSelftTime = config.AppSettings.Settings["YaZhiDialogBoxRenderSelftTime"].Value;
            YaZhiProjectFileName = config.AppSettings.Settings["YaZhiProjectFileName"].Value;
            TTFPath = config.AppSettings.Settings["TTFPath"].Value;
            ImageCutterControlWidth = config.AppSettings.Settings["ImageCutterControlWidth"].Value;
            ImageCutterControlHeight = config.AppSettings.Settings["ImageCutterControlHeight"].Value;
            DefaultLoginName = config.AppSettings.Settings["DefaultLoginName"].Value;
            DefaultPwd = config.AppSettings.Settings["DefaultPwd"].Value;
            //WorkItemsFolder = Properties.Settings.Default.WorkItemsFolder;
            //TemplateFolder = Properties.Settings.Default.TemplateFolder;
            //YaZhiExePath = Properties.Settings.Default.YaZhiExePath;
            //YaZhiPrepareMakeTime = Properties.Settings.Default.YaZhiPrepareMakeTime;
            //YaZhiMakeBtnName = Properties.Settings.Default.YaZhiMakeBtnName;
            //YaZhiDialogBoxYesBtn = Properties.Settings.Default.YaZhiDialogBoxYesBtn;
            //YaZhiRenderSelftTime = Properties.Settings.Default.YaZhiRenderSelftTime;
            //YaZhiDialogBoxRenderSelftTime = Properties.Settings.Default.YaZhiDialogBoxRenderSelftTime;
            //YaZhiProjectFileName = Properties.Settings.Default.YaZhiProjectFileName;
            //TTFPath = Properties.Settings.Default.TTFPath;
            //ImageCutterControlWidth = Properties.Settings.Default.ImageCutterControlWidth;
            //ImageCutterControlHeight = Properties.Settings.Default.ImageCutterControlHeight;
            //DefaultLoginName = Properties.Settings.Default.DefaultLoginName;
            //DefaultPwd = Properties.Settings.Default.DefaultPwd;
        }
        static YLServerConfig config;
        static object syn = new object();
        public static YLServerConfig GetConfig()
        {
            if (config == null)
            {
                lock (syn)
                {
                    config = new YLServerConfig();
                }
            }
            return config;
        }
        /// <summary>
        /// 保存到配置文件
        /// </summary>
        public void UpdateConfig()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings["WorkItemsFolder"].Value = WorkItemsFolder;
            config.AppSettings.Settings["TemplateFolder"].Value = TemplateFolder;
            config.AppSettings.Settings["YaZhiExePath"].Value = YaZhiExePath;
            config.AppSettings.Settings["YaZhiPrepareMakeTime"].Value = YaZhiPrepareMakeTime;
            config.AppSettings.Settings["YaZhiMakeBtnName"].Value = YaZhiMakeBtnName;
            config.AppSettings.Settings["YaZhiDialogBoxYesBtn"].Value = YaZhiDialogBoxYesBtn;
            config.AppSettings.Settings["YaZhiRenderSelftTime"].Value = YaZhiRenderSelftTime;
            config.AppSettings.Settings["YaZhiDialogBoxRenderSelftTime"].Value = YaZhiDialogBoxRenderSelftTime;
            config.AppSettings.Settings["YaZhiProjectFileName"].Value = YaZhiProjectFileName;
            config.AppSettings.Settings["TTFPath"].Value = TTFPath;
            config.AppSettings.Settings["ImageCutterControlWidth"].Value = ImageCutterControlWidth;
            config.AppSettings.Settings["ImageCutterControlHeight"].Value = ImageCutterControlHeight;
            config.AppSettings.Settings["DefaultLoginName"].Value = DefaultLoginName;
            config.AppSettings.Settings["DefaultPwd"].Value = DefaultPwd;
            config.Save();
            //Properties.Settings.Default.WorkItemsFolder = WorkItemsFolder;
            //Properties.Settings.Default.TemplateFolder = TemplateFolder;
            //Properties.Settings.Default.YaZhiExePath = YaZhiExePath;
            //Properties.Settings.Default.YaZhiPrepareMakeTime = YaZhiPrepareMakeTime;
            //Properties.Settings.Default.YaZhiMakeBtnName = YaZhiMakeBtnName;
            //Properties.Settings.Default.YaZhiDialogBoxYesBtn = YaZhiDialogBoxYesBtn;
            //Properties.Settings.Default.YaZhiRenderSelftTime = YaZhiRenderSelftTime;
            //Properties.Settings.Default.YaZhiDialogBoxRenderSelftTime = YaZhiDialogBoxRenderSelftTime;
            //Properties.Settings.Default.YaZhiProjectFileName = YaZhiProjectFileName;
            //Properties.Settings.Default.TTFPath = TTFPath;

            //Properties.Settings.Default.ImageCutterControlWidth = ImageCutterControlWidth;
            //Properties.Settings.Default.ImageCutterControlHeight = ImageCutterControlHeight;
            //Properties.Settings.Default.DefaultLoginName = DefaultLoginName;
            //Properties.Settings.Default.DefaultPwd = DefaultPwd;
            //Properties.Settings.Default.Save();
            lock (syn)
            {
                config = null;
            }
        }
    }
}
